<?php

namespace App\Http\Service;

use App\Models\Userworkfollows;
use App\Models\Doubanpost;
use App\Models\Baoliaopost;
use App\Http\Service\UserService;
use App\Http\Service\BaseService;
use App\Http\Service\ChatgptService;
use Illuminate\Support\Facades\DB;
class UserworkfollowsService  extends BaseService
{
    private $model;

    public function __construct(Userworkfollows $model,ChatgptService $chatgptService,Doubanpost  $doubanpost,UserService $userService,Baoliaopost $baoliaopost)
    {
        parent::__construct();
        $this->model = $model;
        $this->doubanpost=$doubanpost;
        $this->userService=$userService;
        $this->baoliaopost=$baoliaopost;
       
    }
    

    
    

    public  function getList($request,$data=[])
    {
        $page = intval($request->json('page')) ?$request->json('page') :$this->cpage;
        $limit = intval($request->json('limit')) ?$request->json('limit') :$this->climit;
        $offset = ($page-1)*$limit;
        $where = [
           ['status','=','1'],
        ];

        if(isset($data['people_id']) && !empty($data['people_id'])){
            $where[]=['user_id','=',$data['people_id']];
        }
        if(isset($data['type']) && !empty($data['type'])){
            $where[]=['type','=',$data['type']];
        }
        // 计算总数
        $count = $this->model->where($where)->count();
        $fields = ['id','user_id','work_id','type','status','create_time','update_time'];
        $list=$this->model->where($where)->select()
                        ->offset($offset)->limit($limit)
                        ->orderBy('update_time', 'desc')
                        ->orderBy('id', 'desc')
                        ->get()->toArray();
/*        $getUserData= $this->userService->getUserData();
        foreach ($list as &$v) {
            $people_id=$v['following_id'];
            if(isset($getUserData[$people_id])){
                $userData=$getUserData[$people_id];
                $v['nickname']=$userData['nickname'];
                $v['avatar']=$userData['avatar'];
            }

        }*/
        $total_page=count($list);
        $result=[
            'page'=>$page,
            'limit'=>$limit,
            'count'=>$count,
            'total_page'=>$total_page,
            'data'=>$list,
        ];
       // dd($result);
        return $result;

        
    }

    /**
     * 添加
     * @param array $params
     * @return array
     */
    public function addDate($request,$data=[])
    {

        $user_id=$data['user_id'];
        $work_id=$data['work_id'];
        $type=$data['type'];
        $first=$this->model->where(['user_id'=>$user_id,'work_id'=>$work_id,'type'=>$type])->first();
        if($first){
            $data['update_time']=date("Y-m-d H:i:s");
            if($first['status']==2){
                $data['status']=1;//关注
            }else{
                $data['status']=2;//取消关注
            }
            $this->model->where(['user_id'=>$user_id,'work_id'=>$work_id,'type'=>$type])->update($data);
        }else{
            $data['create_time']=date("Y-m-d H:i:s");
            $data['update_time']=date("Y-m-d H:i:s");
            $data['status']=1;
            $this->model->insertGetId($data);
        }

        return($data['status']);
        
        
    }
   

  
}
